//
//  HQValanceSearchViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/6/19.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQValanceSearchViewController.h"


@interface HQValanceSearchViewController ()
{
    NSMutableArray *_imageList;
    BOOL _jiaGeHeSuanTag;
    __weak IBOutlet NSLayoutConstraint *_bottomConstraint;
        __weak IBOutlet NSLayoutConstraint *_bottomConstraint2;
}
@end

@implementation HQValanceSearchViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //写入这个方法后,这个页面将没有这种效果
    [IQKeyboardManager sharedManager].enable = NO;
    //点击背景收回键盘
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //最后还设置回来,不要影响其他页面的效果
    [IQKeyboardManager sharedManager].enable = YES;
    //点击背景收回键盘
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = NO;
}

/**
 打开价格核算

 @param sender 按钮
 */
- (IBAction)openJGHSItem:(id)sender {
    if(!_jiaGeHeSuanTag){
        _jiaGeHeSuanTag =!_jiaGeHeSuanTag;
    }
    if(_jiaGeHeSuanTag){
        [sender setTitle:@"价格核算中..." forState:UIControlStateNormal];
        for(HQValanceModel *s in _dataSource){
            s.isOpenHeSuan = @"YES";
        }
    }
    [_tableView reloadData];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"下联计算";
    
    _jsyl2Item.hidden = YES;
    _hsjgItem.hidden = YES;
    
    [_itemImage setImageWithURL:[NSURL URLWithString:self.dataInfo[@"txl_img"]] placeholderImage:[UIImage imageNamed:@""]];
    _itemName.text = [NSString stringWithFormat:@"  %@  ",self.dataInfo[@"txl_name"]];
    _itemDetail.text = [NSString stringWithFormat:@"样式说明:%@",self.dataInfo[@"txl_desc"]];
    
    _dataSource = [[NSMutableArray alloc]init];
    
    [self searchStyleInfo];

[self addKeyBoardObserver];
}

//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification{
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    _bottomConstraint.constant =keyboardRect.size.height;
        _bottomConstraint2.constant = keyboardRect.size.height;
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{
    _bottomConstraint.constant = 0;
    _bottomConstraint2.constant = 0;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if(_dataSource.count > 0){
        return 2;
    }else{
        return 1;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return _dataSource.count;
    }else{
        return 1;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        HQValanceModel *model = _dataSource[indexPath.row];
        if([model.v_isdelete isEqualToString:@"NO"]){
            HQValanceDefaultSearchCell *cell = [HQValanceDefaultSearchCell cellWithTableView:tableView];
            cell.dataInfo = model;
            cell.tag = 1000 + indexPath.row;
            [cell.v_name addTarget:self action:@selector(selectItem:) forControlEvents:UIControlEventTouchUpInside];
            [cell.v_price addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
            [cell.v_num addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
            [cell.v_artno addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
            return cell;
        }else{
            HQValanceEditCell *cell = [HQValanceEditCell cellWithTableView:tableView];
            cell.tag = 1000 + indexPath.row;
            cell.dataInfo = model;
            [cell.deleteItem addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
            [cell.zuJianItem addTarget:self action:@selector(zujianItem:) forControlEvents:UIControlEventTouchUpInside];
            [cell.v_price addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
            [cell.v_num addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
            [cell.v_artno addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
            return cell;
        }
    }else{
        static NSString *cellId = @"photo";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if(!cell){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.backgroundColor = [UIColor clearColor];
            CGFloat width = self.view.frame.size.width;
            HXPhotoView *photoView = [HXPhotoView photoManager:self.manager];
            photoView.frame = CGRectMake(kPhotoViewMargin, kPhotoViewMargin, width - kPhotoViewMargin * 2, 260);
            photoView.delegate = self;
            photoView.outerCamera = YES;
            photoView.previewShowDeleteButton = YES;
            //    photoView.hideDeleteButton = YES;
            photoView.showAddCell = YES;
            [photoView.collectionView reloadData];
            photoView.backgroundColor = [UIColor clearColor];
            [cell addSubview:photoView];
            self.photoView = photoView;
//            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"相册/相机" style:UIBarButtonItemStylePlain target:self action:@selector(didNavBtnClick)];
            [cell addSubview:self.bottomView];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

/**
 文本框输入控制
 */
-(void)textChange{
    int j =0;
    for(HQValanceModel *sty in _dataSource){
        if([sty.v_isdelete isEqualToString:@"NO"]){
            HQValanceDefaultSearchCell * cell = [_tableView viewWithTag:1000+j];
            sty.v_price = cell.v_price.text;
            sty.v_artno = cell.v_artno.text;
            sty.v_num = cell.v_num.text;
//            sty.v_name = cell.v_name.titleLabel.text;
            sty.v_unit = cell.v_unit.text;
        }else{
            HQValanceEditCell * cell = [_tableView viewWithTag:1000+j];
            sty.v_price = cell.v_price.text;
            sty.v_artno = cell.v_artno.text;
            sty.v_num = cell.v_num.text;
//            sty.v_name = cell.v_name.titleLabel.text;
            sty.v_unit = cell.v_unit.text;
        }
       j++;
    }
    
    int i =0;
    for(HQValanceModel *sty in _dataSource){
        if([sty.v_isdelete isEqualToString:@"NO"]){
            HQValanceEditCell * cell = [_tableView viewWithTag:1000+i];
            sty.v_price = cell.v_price.text;
            sty.v_num = cell.v_num.text;
        }else{
            HQValanceDefaultSearchCell * cell = [_tableView viewWithTag:1000+i];
            sty.v_price = cell.v_price.text;
            sty.v_num = cell.v_num.text;
        }
        i++;
    }
    float allMoney = 0.0;
    for(int i = 0;i < _dataSource.count;i++){
        UITableViewCell * cell = [_tableView viewWithTag:1000+i];
        HQValanceModel *sty = _dataSource[i];
        if([cell isKindOfClass:[HQValanceDefaultSearchCell class]]){
            HQValanceDefaultSearchCell *d1Cell = (HQValanceDefaultSearchCell *)cell;
            sty.v_price = d1Cell.v_price.text;
            sty.v_num = sty.v_num;
        }else if([cell isKindOfClass:[HQValanceEditCell class]]){
            HQValanceEditCell *d2Cell = (HQValanceEditCell *)cell;
            sty.v_price = d2Cell.v_price.text;
            sty.v_num = d2Cell.v_num.text;
        }
        float temNum = [sty.v_num floatValue] * [sty.v_price floatValue];
        allMoney = allMoney + temNum;
    }
    _totalMoney = [NSString stringWithFormat:@"%.2f",allMoney];
    [_totalMoneyItem setTitle:[NSString stringWithFormat:@"总额：%@元",_totalMoney] forState:UIControlStateNormal];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        return 44;
    }else{
        return SCREEN_WIDTH + 32;
    }
}

// 显示核算结果视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if(section == 0){
        if(_dataSource.count>0){
            HeSuanHeaderView *h = [HeSuanHeaderView loadTopHotView];
            h.heSuanBgView.hidden = YES;
            h.delegate = self;
            h.heSuanConstraint.constant = 0;
            return h;
        }else{
            return 0;
        }
    }else{
        return 0;
    }
}

// 显示核算结果视图
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if(section == 0){
        if(_dataSource.count >0){
            return 70;
        }else{
            return 0;
        }
    }else{
        return 0;
    }
}

/**
 获取样式详情数据
 */
-(void)searchStyleInfo{
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam get_xl_info_xlid:self.dataInfo[@"txl_id"]] withMethod:[HQMethod get_xl_info] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            NSLog(@"==== %@",sucData[@"data"]);
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 计算用料
 
 @param sender 按钮
 */
- (IBAction)excueItem:(id)sender {
    if([self isInputEmpty:_clkdField.text]){
        [self tostView:@"请输入窗帘宽度"];
        return;
    }else if([self isInputEmpty:_clgdField.text]){
        [self tostView:@"请输入窗帘高度"];
        return;
    }else if([self isInputEmpty:_clblField.text]){
        [self tostView:@"请输入窗帘倍率"];
        return;
    }
//    else if([self isInputEmpty:_blhhjField.text]){
//        [self tostView:@"请输入布料横花距"];
//        return;
//    }
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam get_pro_excue_pldgmk_clkd:_clkdField.text clgd:_clgdField.text clbl:_clblField.text blhhj:_blhhjField.text ysid:self.dataInfo[@"txl_id"] uid:[UserModelManager userID]] withMethod:[HQMethod get_pro_excue] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            NSDictionary *dict= sucData[@"data"];
            NSLog(@"第一个样式数据 %@",dict);
            _jsyl2Item.hidden = NO;
            _hsjgItem.hidden = NO;
            _jsyl1Item.hidden = YES;
            
            [_dataSource removeAllObjects];
            
            _zuJianArray = dict[@"zujian"];
            _banDaiArray = dict[@"bandai"];
            _huaBianArray = dict[@"huabian"];
            
            // 主布
            NSDictionary *zhubu = dict[@"zhubu"];
            HQValanceModel *m1 = [[HQValanceModel alloc]init];
            m1.v_unit = zhubu[@"danwei"];
            m1.v_name = zhubu[@"txt"];
            m1.xzn_ename = zhubu[@"xzn_ename"];
            m1.v_num = [NSString stringWithFormat:@"%@", zhubu[@"val"]];
            m1.v_isdelete = @"NO";
            m1.v_istitleEdit = @"NO";
            
            // 布带
            NSDictionary *budai = dict[@"budai"];
            HQValanceModel *m2 = [[HQValanceModel alloc]init];
            m2.v_unit = budai[@"danwei"];
            m2.v_name = budai[@"txt"];
            m2.xzn_ename = budai[@"xzn_ename"];
            m2.v_num = [NSString stringWithFormat:@"%@", budai[@"val"]];
            m2.v_isdelete = @"NO";
            m2.v_istitleEdit = @"NO";
            
            // 罗马圈
            HQValanceModel *m3 = [[HQValanceModel alloc]init];
            m3.v_isdelete = @"NO";
            m3.v_istitleEdit = @"YES";
            m3.v_name = @"选择板带";
            m3.sourceMark = @"bandai";
            
            // 花边
            HQValanceModel *m4 = [[HQValanceModel alloc]init];
            m4.v_isdelete = @"NO";
            m4.v_istitleEdit = @"YES";
            m4.v_name = @"选择花边";
            m4.sourceMark = @"huabian";
            
            [_dataSource addObject:m1];
            [_dataSource addObject:m2];
            [_dataSource addObject:m3];
            [_dataSource addObject:m4];
            [_tableView reloadData];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/************************************************添加自定义组件数据************************************************/
/**
 添加一条自定义样式数据
 */
-(void)addCell{
    int i =0;
    for(HQValanceModel *sty in _dataSource){
        if([sty.v_isdelete isEqualToString:@"NO"]){
            HQValanceDefaultSearchCell * cell = [_tableView viewWithTag:1000+i];
            if(_jiaGeHeSuanTag){
                sty.isOpenHeSuan = @"YES";
            }else{
                sty.isOpenHeSuan = @"NO";
            }
            sty.v_price = cell.v_price.text;
            sty.v_artno = cell.v_artno.text;
            sty.v_num = cell.v_num.text;
            sty.v_name = cell.v_name.titleLabel.text;
            sty.v_unit = cell.v_unit.text;
        }else{
            HQValanceEditCell * cell = [_tableView viewWithTag:1000+i];
            if(_jiaGeHeSuanTag){
                sty.isOpenHeSuan = @"YES";
            }else{
                sty.isOpenHeSuan = @"NO";
            }
            sty.v_price = cell.v_price.text;
            sty.v_artno = cell.v_artno.text;
            sty.v_num = cell.v_num.text;
            sty.v_name = cell.v_name.titleLabel.text;
            sty.v_unit = cell.v_unit.text;
        }
        i++;
    }

    HQValanceModel *m = [[HQValanceModel alloc]init];
    m.v_isdelete = @"YES";
    if(_jiaGeHeSuanTag){
        m.isOpenHeSuan = @"YES";
    }else{
        m.isOpenHeSuan = @"NO";
    }
    m.v_name = @"请选择";
    [_dataSource addObject:m];
    [_tableView reloadData];
}

-(void)heSuanJiaGe{}

/**
 删除按钮
 
 @param button 按钮
 */
-(void)deleteItem:(HQButton *)button{
    [_dataSource removeObject:button.dataInfo];
    [_tableView reloadData];
    [self textChange];
}

/**
 选择板带、花边
 
 @param button 按钮
 */
-(void)selectItem:(HQButton *)button{
    _currentSearchCell = button.className;
    HQValanceModel *m = button.dataInfo;
    // 列表中有值，且已经输入过数据
    [self.view endEditing:YES];
    if([m.sourceMark isEqualToString:@"bandai"]){
        NSMutableArray *m = [[NSMutableArray alloc]init];
        for(NSDictionary *dic in _banDaiArray){
            [m addObject:[NSString stringWithFormat:@"%@",dic[@"xzn_name"]]];
        }
        ZHPickView *_zhPickerView = [[ZHPickView alloc]initPickviewWithArray:m isHaveNavControler:NO];
        [_zhPickerView setPickeTitle:@"选择板带"];
        [_zhPickerView selectObj:_currentSearchCell.v_name.titleLabel.text];
        _zhPickerView.delegate = self;
        _zhPickerView.tag = 2001;
        [_zhPickerView show];
    }else if([m.sourceMark isEqualToString:@"huabian"]){
        NSMutableArray *m = [[NSMutableArray alloc]init];
        for(NSDictionary *dic in _huaBianArray){
            [m addObject:[NSString stringWithFormat:@"%@",dic[@"xzn_name"]]];
        }
        ZHPickView *_zhPickerView = [[ZHPickView alloc]initPickviewWithArray:m isHaveNavControler:NO];
        [_zhPickerView setPickeTitle:@"选择花边"];
        [_zhPickerView selectObj:_currentSearchCell.v_name.titleLabel.text];
        _zhPickerView.delegate = self;
        _zhPickerView.tag = 2002;
        [_zhPickerView show];
    }
}

/**
 选择组件
 
 @param button 按钮
 */
-(void)zujianItem:(HQButton *)button{
    _currentCell = button.className;
    // 列表中有值，且已经输入过数据
    [self.view endEditing:YES];
    NSMutableArray *m = [[NSMutableArray alloc]init];
    for(NSDictionary *dic in _zuJianArray){
        [m addObject:[NSString stringWithFormat:@"%@",dic[@"xzn_name"]]];
    }
    ZHPickView *_zhPickerView = [[ZHPickView alloc]initPickviewWithArray:m isHaveNavControler:NO];
    [_zhPickerView setPickeTitle:@"样式组件"];
    [_zhPickerView selectObj:_currentCell.v_name.titleLabel.text];
    _zhPickerView.delegate = self;
    _zhPickerView.tag = 2000;
    [_zhPickerView show];
}

-(void)toobarDonBtnHaveClick:(ZHPickView *)pickView resultString:(NSString *)resultString{
    NSString *tempID = @"";
    NSString *tempName = @"";
    NSString *tempUnit = @"";
    NSString *tempENName = @"";
    if(pickView.tag == 2000){
        for(NSDictionary *dic in _zuJianArray){
            NSString *pattonStr = [NSString stringWithFormat:@"%@",dic[@"xzn_name"]];
            if([pattonStr isEqualToString:resultString]){
                tempID = dic[@"xzn_id"];
                tempName = dic[@"xzn_name"];
                tempUnit = dic[@"xzn_danwei"];
                tempENName = dic[@"xzn_ename"];
                break;
            }
        }
        HQValanceModel *s = _currentCell.dataInfo;
        s.v_id = tempID;
        s.v_unit = tempUnit;
        s.xzn_ename = tempENName;
        s.v_name = tempName;
        _currentCell.v_unit.text = tempUnit;
        [_currentCell.v_name setTitle:resultString forState:UIControlStateNormal];
        [self textChange];
    }else if(pickView.tag == 2001){
        for(NSDictionary *dic in _banDaiArray){
            NSString *pattonStr = [NSString stringWithFormat:@"%@",dic[@"xzn_name"]];
            if([pattonStr isEqualToString:resultString]){
                tempID = dic[@"xzn_id"];
                tempName = dic[@"xzn_name"];
                tempUnit = dic[@"xzn_danwei"];
                tempENName = dic[@"xzn_ename"];
                break;
            }
        }
        [self showHudText:@""];
        HQValanceModel *m =_dataSource[1];
        [HQNetWork requetParam:[HQParam get_bandai_size_ename:tempENName bdsize:[NSString stringWithFormat:@"%@",m.v_num] ysid:self.dataInfo[@"txl_id"] uid:[UserModelManager userID]] withMethod:[HQMethod get_bandai_size] suc:^(NSDictionary *sucData) {
            [self hiddnHud];
            if([sucData[kStatus]integerValue] == 1){
                NSDictionary *dict= sucData[@"data"];
                HQValanceModel *s = _currentSearchCell.dataInfo;
                s.xzn_ename = tempENName;
                s.v_id = tempID;
                s.v_unit = tempUnit;
                s.v_num = [NSString stringWithFormat:@"%@",dict[@"val"]];
                s.v_name = tempName;
                _currentSearchCell.v_num.text = [NSString stringWithFormat:@"%@",dict[@"val"]];
                _currentSearchCell.v_unit.text = tempUnit;
                [_currentSearchCell.v_name setTitle:resultString forState:UIControlStateNormal];
                [self textChange];
            }else{
                [self tostView:sucData[kInfo]];
            }
        } fail:^{
            [self hiddnHud];
        }];
    }else if(pickView.tag == 2002){
        for(NSDictionary *dic in _huaBianArray){
            NSString *pattonStr = [NSString stringWithFormat:@"%@",dic[@"xzn_name"]];
            if([pattonStr isEqualToString:resultString]){
                tempID = dic[@"xzn_id"];
                tempName = dic[@"xzn_name"];
                tempUnit = dic[@"xzn_danwei"];
                tempENName = dic[@"xzn_ename"];
                break;
            }
        }
        [self showHudText:@""];
        HQValanceModel *m =_dataSource[1];
        [HQNetWork requetParam:[HQParam get_huabian_size_ename:tempENName bdsize:[NSString stringWithFormat:@"%@",m.v_num] clgd:_clgdField.text ysid:self.dataInfo[@"txl_id"] uid:[UserModelManager userID]] withMethod:[HQMethod get_huabian_size] suc:^(NSDictionary *sucData) {
            [self hiddnHud];
            if([sucData[kStatus]integerValue] == 1){
                NSDictionary *dict= sucData[@"data"];
                HQValanceModel *s = _currentSearchCell.dataInfo;
                s.xzn_ename = tempENName;
                s.v_id = tempID;
                s.v_unit = tempUnit;
                s.v_num = [NSString stringWithFormat:@"%@",dict[@"val"]];
                s.v_name = tempName;
                _currentSearchCell.v_num.text = [NSString stringWithFormat:@"%@",dict[@"val"]];
                _currentSearchCell.v_unit.text = tempUnit;
                [_currentSearchCell.v_name setTitle:resultString forState:UIControlStateNormal];
                [self textChange];
            }else{
                [self tostView:sucData[kInfo]];
            }
        } fail:^{
            [self hiddnHud];
        }];
    }
}

/**
 查看样式大图
 
 @param sender 查看样式大图
 */
- (IBAction)lookBigImage:(id)sender {
    HQBigImageViewController *bigImageVc = [[HQBigImageViewController alloc]init];
    bigImageVc.imageURL = self.dataInfo[@"txl_img"];
    [self presentViewController:bigImageVc animated:YES completion:nil];
}

/**
 保存记录
 
 @param sender 按钮
 */
- (IBAction)saveRecord:(id)sender {
    if([self isInputEmpty:_clkdField.text]){
        [self tostView:@"请输入窗帘宽度"];
        return;
    }else if([self isInputEmpty:_clgdField.text]){
        [self tostView:@"请输入窗帘高度"];
        return;
    }else if([self isInputEmpty:_clblField.text]){
        [self tostView:@"请输入窗帘倍率"];
        return;
    }
//    else if([self isInputEmpty:_blhhjField.text]){
//        [self tostView:@"请输入布料横花距"];
//        return;
//    }
    else if([self isInputEmpty:_userName.text]){
        [self tostView:@"请输入客户姓名"];
        return;
    }else if([self isInputEmpty:_userPhone.text]){
        [self tostView:@"请输入手机号"];
        return;
    }else if(![HQTool phoneNoRegex:_userPhone.text]){
        [self tostView:@"请输入正确的手机号"];
        return;
    }
    for(HQValanceModel *sty in _dataSource){
        NSLog(@"%@ = %@ = %@ = %@",sty.xzn_ename,sty.v_num,sty.v_artno,sty.v_price);
        if([self isInputEmpty:sty.xzn_ename]||[self isInputEmpty:sty.v_num]||[self isInputEmpty:sty.v_artno]||[self isInputEmpty:sty.v_price]){
            [self tostView:@"界面中有未填项,请填写后提交"];
            return;
        }
    }
    
    // 计算结果
    NSMutableArray *excuResArr = [[NSMutableArray alloc]init];
    NSMutableArray *imgArr = [[NSMutableArray alloc]init];
    if(_imageList.count>0){
        for(UIImage *img in _imageList){
            [self showHudText:@"上传图片..."];
            HQNetWork *hqnetwork = [[HQNetWork alloc]init];
            [hqnetwork httpUploadImages:img URL:[HQMethod uploadface] param:nil suc:^(NSDictionary *suc) {
                [self hiddnHud];
                if([suc[kStatus]integerValue] == 1){
                    NSString *imageUrl = [[suc objectForKey:@"data"] objectForKey:@"imgurl"];
                    [imgArr addObject:imageUrl];
                    if(imgArr.count == _imageList.count){
                        int i = 0;
                        for(HQValanceModel *sty in _dataSource){
                            NSLog(@"ename= %@",sty.xzn_ename);
                            if([sty.v_isdelete isEqualToString:@"NO"]){
                                HQValanceDefaultSearchCell *cell = [_tableView viewWithTag:1000+i];
                                NSDictionary *dict = @{@"xzn_ename":sty.xzn_ename,@"val":cell.v_num.text,@"bianhao":cell.v_artno.text,@"danjia":cell.v_price.text};
                                [excuResArr addObject:dict];
                            }else{
                                HQValanceEditCell * cell = [_tableView viewWithTag:1000+i];
                                NSDictionary *dict = @{@"xzn_ename":sty.xzn_ename,@"val":cell.v_num.text,@"bianhao":cell.v_artno.text,@"danjia":cell.v_price.text};
                                [excuResArr addObject:dict];
                            }
                            i++;
                        }
                        
                        // 搜索条件
                        NSMutableDictionary *searchhdt = [[NSMutableDictionary alloc]init];
                        [searchhdt setValue:_clkdField.text forKey:@"clkd"];
                        [searchhdt setValue:_clgdField.text forKey:@"clgd"];
                        [searchhdt setValue:_clblField.text forKey:@"clbl"];
                        [searchhdt setValue:_blhhjField.text forKey:@"blhhj"];
                        
                        // 图片接口
                        NSArray *images = imgArr;
                        [self showHudText:@""];
                        [HQNetWork requetParam:[HQParam save_xlorderName:_userName.text withPhone:_userPhone.text withSId:self.dataInfo[@"txl_id"] withUId:[UserModelManager userID] withPrice:_totalMoney withSearchdt:searchhdt withExcuRes:excuResArr withImages:images] withMethod:[HQMethod save_xlorder] suc:^(NSDictionary *sucData) {
                            [self hiddnHud];
                            if([sucData[kStatus]integerValue] == 1){
                                NSString *infoStr = [NSString stringWithFormat:@"%@,请到订单记录界面查看!",sucData[@"info"]];
                                UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:infoStr  preferredStyle:UIAlertControllerStyleAlert];
                                [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}]];
                                [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                                    [self.navigationController popViewControllerAnimated:YES];
                                }]];
                                [self presentViewController:alertController animated:YES completion:nil];
                            }else{
                                [self tostView:sucData[kInfo]];
                            }
                        } fail:^{
                            [self hiddnHud];
                        }];
                    }
                }else{
                    [self tostView:suc[kInfo]];
                }
            } fail:^{
                [self hiddenErrorHud];
            }];
        }
    }else{
        int i = 0;
        for(HQValanceModel *sty in _dataSource){
            NSLog(@"ename= %@",sty.xzn_ename);
            if([sty.v_isdelete isEqualToString:@"NO"]){
                HQValanceDefaultSearchCell *cell = [_tableView viewWithTag:1000+i];
                NSDictionary *dict = @{@"xzn_ename":sty.xzn_ename,@"val":cell.v_num.text,@"bianhao":cell.v_artno.text,@"danjia":cell.v_price.text};
                [excuResArr addObject:dict];
            }else{
                HQValanceEditCell * cell = [_tableView viewWithTag:1000+i];
                NSDictionary *dict = @{@"xzn_ename":sty.xzn_ename,@"val":cell.v_num.text,@"bianhao":cell.v_artno.text,@"danjia":cell.v_price.text};
                [excuResArr addObject:dict];
            }
            i++;
        }
        
        // 搜索条件
        NSMutableDictionary *searchhdt = [[NSMutableDictionary alloc]init];
        [searchhdt setValue:_clkdField.text forKey:@"clkd"];
        [searchhdt setValue:_clgdField.text forKey:@"clgd"];
        [searchhdt setValue:_clblField.text forKey:@"clbl"];
        [searchhdt setValue:_blhhjField.text forKey:@"blhhj"];
        
        // 图片接口
        NSArray *images = imgArr;
        [self showHudText:@""];
        [HQNetWork requetParam:[HQParam save_xlorderName:_userName.text withPhone:_userPhone.text withSId:self.dataInfo[@"txl_id"] withUId:[UserModelManager userID] withPrice:_totalMoney withSearchdt:searchhdt withExcuRes:excuResArr withImages:images] withMethod:[HQMethod save_xlorder] suc:^(NSDictionary *sucData) {
            [self hiddnHud];
            if([sucData[kStatus]integerValue] == 1){
                NSString *infoStr = [NSString stringWithFormat:@"%@,请到订单记录界面查看!",sucData[@"info"]];
                UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:infoStr  preferredStyle:UIAlertControllerStyleAlert];
                [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}]];
                [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                    [self.navigationController popViewControllerAnimated:YES];
                }]];
                [self presentViewController:alertController animated:YES completion:nil];
            }else{
                [self tostView:sucData[kInfo]];
            }
        } fail:^{
            [self hiddnHud];
        }];
    }
}

- (HXDatePhotoToolManager *)toolManager {
    if (!_toolManager) {
        _toolManager = [[HXDatePhotoToolManager alloc] init];
    }
    return _toolManager;
}

- (HXPhotoManager *)manager {
    if (!_manager) {
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhotoAndVideo];
        _manager.configuration.openCamera = YES;
        _manager.configuration.lookLivePhoto = YES;
        _manager.configuration.photoMaxNum = 9;
        _manager.configuration.videoMaxNum = 1;
        _manager.configuration.maxNum = 10;
        _manager.configuration.videoMaxDuration = 500.f;
        _manager.configuration.saveSystemAblum = NO;
        //        _manager.configuration.reverseDate = YES;
        _manager.configuration.showDateSectionHeader = NO;
        _manager.configuration.selectTogether = NO;
        //        _manager.configuration.rowCount = 3;
        //        _manager.configuration.movableCropBox = YES;
        //        _manager.configuration.movableCropBoxEditSize = YES;
        //        _manager.configuration.movableCropBoxCustomRatio = CGPointMake(1, 1);
        _manager.configuration.requestImageAfterFinishingSelection = YES;
        __weak typeof(self) weakSelf = self;
        //        _manager.configuration.replaceCameraViewController = YES;
        _manager.configuration.shouldUseCamera = ^(UIViewController *viewController, HXPhotoConfigurationCameraType cameraType, HXPhotoManager *manager) {
            
            // 这里拿使用系统相机做例子
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            imagePickerController.delegate = (id)weakSelf;
            imagePickerController.allowsEditing = NO;
            NSString *requiredMediaTypeImage = ( NSString *)kUTTypeImage;
            NSString *requiredMediaTypeMovie = ( NSString *)kUTTypeMovie;
            NSArray *arrMediaTypes;
            if (cameraType == HXPhotoConfigurationCameraTypePhoto) {
                arrMediaTypes=[NSArray arrayWithObjects:requiredMediaTypeImage,nil];
            }else if (cameraType == HXPhotoConfigurationCameraTypeVideo) {
                arrMediaTypes=[NSArray arrayWithObjects:requiredMediaTypeMovie,nil];
            }else {
                arrMediaTypes=[NSArray arrayWithObjects:requiredMediaTypeImage, requiredMediaTypeMovie,nil];
            }
            [imagePickerController setMediaTypes:arrMediaTypes];
            // 设置录制视频的质量
            [imagePickerController setVideoQuality:UIImagePickerControllerQualityTypeHigh];
            //设置最长摄像时间
            [imagePickerController setVideoMaximumDuration:60.f];
            imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePickerController.navigationController.navigationBar.tintColor = [UIColor whiteColor];
            imagePickerController.modalPresentationStyle=UIModalPresentationOverCurrentContext;
            [viewController presentViewController:imagePickerController animated:YES completion:nil];
        };
    }
    return _manager;
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    HXPhotoModel *model;
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        model = [HXPhotoModel photoModelWithImage:image];
        if (self.manager.configuration.saveSystemAblum) {
            [HXPhotoTools savePhotoToCustomAlbumWithName:self.manager.configuration.customAlbumName photo:model.thumbPhoto];
        }
    }else  if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
        NSURL *url = info[UIImagePickerControllerMediaURL];
        NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]
                                                         forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
        AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:url options:opts];
        float second = 0;
        second = urlAsset.duration.value/urlAsset.duration.timescale;
        model = [HXPhotoModel photoModelWithVideoURL:url videoTime:second];
        if (self.manager.configuration.saveSystemAblum) {
            [HXPhotoTools saveVideoToCustomAlbumWithName:self.manager.configuration.customAlbumName videoURL:url];
        }
    }
    if (self.manager.configuration.useCameraComplete) {
        self.manager.configuration.useCameraComplete(model);
    }
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)didNavBtnClick {
    if (self.manager.configuration.specialModeNeedHideVideoSelectBtn && !self.manager.configuration.selectTogether && self.manager.configuration.videoMaxNum == 1) {
        if (self.manager.afterSelectedVideoArray.count) {
            [self.view showImageHUDText:@"请先删除视频"];
            return;
        }
    }
    [self.photoView goPhotoViewController];
}

- (void)photoView:(HXPhotoView *)photoView changeComplete:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photos videos:(NSArray<HXPhotoModel *> *)videos original:(BOOL)isOriginal {
    //    NSSLog(@"所有:%ld - 照片:%ld - 视频:%ld",allList.count,photos.count,videos.count);
    //    NSSLog(@"所有:%@ - 照片:%@ - 视频:%@",allList,photos,videos);
    
    // 获取图片
    [self.toolManager getSelectedImageList:allList requestType:HXDatePhotoToolManagerRequestTypeOriginal success:^(NSArray<UIImage *> *imageList) {
        
    } failed:^{
        
    }];
    
    //    [HXPhotoTools selectListWriteToTempPath:allList requestList:^(NSArray *imageRequestIds, NSArray *videoSessions) {
    //        NSSLog(@"requestIds - image : %@ \nsessions - video : %@",imageRequestIds,videoSessions);
    //    } completion:^(NSArray<NSURL *> *allUrl, NSArray<NSURL *> *imageUrls, NSArray<NSURL *> *videoUrls) {
    //        NSSLog(@"allUrl - %@\nimageUrls - %@\nvideoUrls - %@",allUrl,imageUrls,videoUrls);
    //    } error:^{
    //        NSSLog(@"失败");
    //    }];
}

- (void)photoView:(HXPhotoView *)photoView imageChangeComplete:(NSArray<UIImage *> *)imageList {
    NSSLog(@"%@",imageList);
    _imageList = [[NSMutableArray alloc]initWithArray:imageList];
}

- (void)photoView:(HXPhotoView *)photoView deleteNetworkPhoto:(NSString *)networkPhotoUrl {
    NSSLog(@"%@",networkPhotoUrl);
}

- (void)photoView:(HXPhotoView *)photoView updateFrame:(CGRect)frame {
    NSSLog(@"%@",NSStringFromCGRect(frame));
    //    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, CGRectGetMaxY(frame) + kPhotoViewMargin);
    
}

- (void)photoView:(HXPhotoView *)photoView currentDeleteModel:(HXPhotoModel *)model currentIndex:(NSInteger)index {
    NSSLog(@"%@ --> index - %ld",model,index);
}

- (BOOL)photoViewShouldDeleteCurrentMoveItem:(HXPhotoView *)photoView {
    return self.needDeleteItem;
}
- (void)photoView:(HXPhotoView *)photoView gestureRecognizerBegan:(UILongPressGestureRecognizer *)longPgr indexPath:(NSIndexPath *)indexPath {
    [UIView animateWithDuration:0.25 animations:^{
        self.bottomView.alpha = 0.5;
    }];
    NSSLog(@"长按手势开始了 - %ld",indexPath.item);
}
- (void)photoView:(HXPhotoView *)photoView gestureRecognizerChange:(UILongPressGestureRecognizer *)longPgr indexPath:(NSIndexPath *)indexPath {
    CGPoint point = [longPgr locationInView:self.view];
    if (point.y >= self.bottomView.hx_y) {
        [UIView animateWithDuration:0.25 animations:^{
            self.bottomView.alpha = 1;
        }];
    }else {
        [UIView animateWithDuration:0.25 animations:^{
            self.bottomView.alpha = 0.5;
        }];
    }
    NSSLog(@"长按手势改变了 %@ - %ld",NSStringFromCGPoint(point), indexPath.item);
}
- (void)photoView:(HXPhotoView *)photoView gestureRecognizerEnded:(UILongPressGestureRecognizer *)longPgr indexPath:(NSIndexPath *)indexPath {
    CGPoint point = [longPgr locationInView:self.view];
    if (point.y >= self.bottomView.hx_y) {
        self.needDeleteItem = YES;
        [self.photoView deleteModelWithIndex:indexPath.item];
    }else {
        self.needDeleteItem = NO;
    }
    NSSLog(@"长按手势结束了 - %ld",indexPath.item);
    [UIView animateWithDuration:0.25 animations:^{
        self.bottomView.alpha = 0;
    }];
}


@end
